package cn.nawang.ebeim.uc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.PropertyConfigurator;

import cn.nawang.ebeim.client.util.PathUtil;

public class SystemUtil {

  private static final String CONF_PATH = "conf";
  private static final String BIN_PATH = "bin";

  public static String getRootPath() throws Exception {
    String path =  pathEncode(Class.class.getClass().getResource("/").getPath());
    return path;
  }
  
  public static String getRootPath(Object obj) throws Exception {
    String path =  pathEncode(obj.getClass().getResource("/").getPath());
    return path;
  }

  public static String getConfPath() throws Exception {
    String rootPath = getRootPath();
    return rootPath + getChangePath(rootPath) + CONF_PATH;
  }
  
  public static String getConfPath(Object obj) throws Exception {
    String rootPath = getRootPath(obj);
    return rootPath + getChangePath(rootPath) + CONF_PATH;
  }
  
  public static String getBinPath(Object obj) throws Exception {
    String rootPath = getRootPath(obj);
    return rootPath + getChangePath(rootPath) + BIN_PATH;
  }
  
  public static String getChangePath(String path) {
    if (path.endsWith("classes") || path.endsWith("classes/")) {
      return "../../";
    }
    return "../";
  }

  public static void initLog() throws Exception {
//    String path = getConfPath() + "/log4j.properties";
    File rootDir = new File(PathUtil.getClassPath());
    String path = rootDir + "/others/conf/log4j.properties";
    path = java.net.URLDecoder.decode(path,"utf-8");//
    PropertyConfigurator.configure(path);
  }
  
  public static String pathEncode(String path) throws UnsupportedEncodingException{
    String configPath = path;
    configPath = java.net.URLDecoder.decode(configPath,"utf-8");
    return configPath;
  }
  
  public static String getConfProPath(){
    File rootDir = new File(PathUtil.getClassPath());
    return rootDir+"/others/conf/config.properties";
//    return rootDir+"/conf/config.properties";
  }

  public static String read(String key) {
    Properties props = new Properties();
    InputStream in = null;
    try {
      // 从目录“程序目录/conf”下加载配置文件
      File rootDir = new File(PathUtil.getClassPath());
      String confPath =  getConfProPath();//生成exe的时候要放开，调试的时候注释掉 
      confPath = java.net.URLDecoder.decode(confPath,"utf-8");//
      in = new FileInputStream(new File(confPath));
      props.load(in);
      String value = props.getProperty(key);
      in.close();
      return value;
    } catch (Exception e) {
      e.printStackTrace();
      if (in != null) {
        try {
          in.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      return null;
    }
  }
  
  /**
   * 删除配置文件中的key
   * @throws IOException
   *
   */
  public static void delete(String delKey) throws IOException{
     Properties properties = new Properties();
     String confPath = getConfProPath();
     confPath = java.net.URLDecoder.decode(confPath,"utf-8");//
     File file = new File(confPath);
     FileInputStream fis = new FileInputStream(file);
     properties.load(fis);   
    
     //必须先用map将所有的内容先保存,不然一删除,原来的内容都没了
     Map<String, String> map = new HashMap<String, String>();
    
     Set<Object> keySet = properties.keySet();
    
     System.out.println(keySet.size());
    
     for (Object object : keySet) {
         String key = (String) object;
         String value = (String) properties.get(key);
         System.out.println(key+"="+value);
         map.put(key, value);
     }
     //删除key
     map.remove(delKey);
     properties.remove(delKey);
    
     for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
         properties.setProperty(entry.getKey(), entry.getValue());
     }
    
     FileOutputStream fos = new FileOutputStream(file);
     properties.store(fos, "delete key :"+delKey);     

     fos.close();
    
     fis.close();
    
  }
  
  
  
  
  

  public static void main(String[] args) throws Exception {
    System.out.println(SystemUtil.getConfPath());
  }

}
